var app = getApp();
var time = require('../../utils/util.js');
Page({
  data: {
    recommendURL: app.globalData.bookUrl + '/Recommend/SaveRecommend.do',
    selectRecommendURL: app.globalData.bookUrl + '/Recommend/selectRecommend.do',
    bookName:"",
    bookAuthor:"",
    currentPage:1,
    recommendList:[],
    totalElements: 0,
    fillFilePaths:"",
    winHeight: 0,
    height:0,
    canvasHeight:"10"
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    // 显示顶部刷新图标
    wx.showNavigationBarLoading({
      title: '正在刷新',
    })
    // 页数+1
    that.getRecommend(1, "yes");
  },
  onReachBottom: function () {
    var that = this;
    var recommendsList = that.data.recommendList;
    if (recommendsList.length == that.data.totalElements) { //最大数值
      wx.showToast({
        title: "已到达底部",
        icon: "success",
        duration: 300
      });
      return false;
    } else {
      // 显示加载图标
      // wx.showLoading({
      //   title: '玩命加载中',
      // })
      // 页数+1
      that.getRecommend(that.data.currentPage, "no");

    }
  },
  // onShow: function (options) {
  //   var that = this;
  //   wx.getStorage({
  //     key: 'openid',
  //     success(res) {
  //       that.setData({
  //         openId: res.data
  //       });
  //     }
  //   })
  //   that.getRecommend(that.data.currentPage, "yes");
  //   /** 获取系统信息 */
  //   // wx.getSystemInfo({
  //   //   success: function (res) {
  //   //     console.log(res.windowHeight)
  //   //     that.setData({
  //   //       winWidth: res.windowWidth,
  //   //       winHeight: res.windowHeight
  //   //     });
  //   //   }
  //   // });
  //   // var query = wx.createSelectorQuery();
  //   // query.select('.button').boundingClientRect()
  //   // query.exec(function (res) {
  //   //   console.log(res[0].height);
  //   //   that.setData({
  //   //     height: res[0].height
  //   //   })
  //   // })
  // },
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openId: res.data
        });
      }
    })
    that.getRecommend(that.data.currentPage, "yes");
    /** 获取系统信息 */
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    var query = wx.createSelectorQuery();
    query.select('.button').boundingClientRect()
    query.exec(function (res) {
      console.log(res[0].height);
      that.setData({
        height: res[0].height
      })
    })
  },
  getRecommend: function (currentPage, refresh){
   
    var that = this;
    if (refresh=="yes"){
      that.setData({
        recommendList: [],
        
      })
      currentPage= 1;
    }
    wx.request({
      url: that.data.selectRecommendURL,
      data: {
        currentPage: currentPage,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.showRecommend(res.data.data); //向前台传递数据
        if (res.data.data != null) {
          that.setData({
            currentPage: currentPage + 1,
            totalElements: res.data.data.totalElements
          });
        }
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  showRecommend: function (recommend) {
    var that = this;

    var recommends = that.data.recommendList;
    if (recommend != null) {
      for (var idx in recommend.content) {
        var content = recommend.content[idx];
        var bookName = content.bookName;
        var bookAuthor = content.bookAuthor;
        var recommendImg = app.globalData.bookUrl + content.recommendImg + '?' + Math.random() / 9999;
        var recommendImgSource = content.recommendImg;
        var allreadersName = content.allreadersName;
        var openid = content.openid;
        var createTime = time.formatTime(content.createTime, 'Y-M-D h:m:s');
        var temp = {
          allreadersName: allreadersName,
          createTime: createTime,
          bookName: bookName,
          recommendImg: recommendImg,
          openid: openid,
          recommendImgSource: recommendImgSource,
          bookAuthor: bookAuthor,
        }
        recommends.push(temp);
      }
      that.setData({
        recommendList: recommends
      });
      wx.hideLoading({
        title: '玩命加载中',
      })
    } else {
      wx.showToast({
        title: "没有新推荐",
        image: '../../images/icon/fail.png',
        duration: 1500
      })
    }
  },
  navigateToDetails: function (e) {
    var bookName = e.currentTarget.dataset.bookname;
    var bookAuthor = e.currentTarget.dataset.bookauthor;
      wx.navigateTo({
        url: "RecommendList/RecommendList?bookName=" + bookName + "&bookAuthor=" + bookAuthor 
      })
  },
    //推荐


  onGotUserInfo: function (e) {
    var that = this;
    var nickName = e.detail.userInfo.nickName;
    var avatarUrl = e.detail.userInfo.avatarUrl;
    that.setData({
      nickName: nickName,
      avatarUrl: avatarUrl
    });
    wx.navigateTo({
      url: 'myRecommend/myRecommend?&recommendToo=1' + "&nickName=" + nickName + "&avatarUrl=" + avatarUrl 
    })
  },
  //我也推荐
  powerDrawerToo: function (e) {
    var that = this;
    var nickName = e.detail.userInfo.nickName;
    var avatarUrl = e.detail.userInfo.avatarUrl;
    var bookName = e.currentTarget.dataset.bookname;
    var bookAuthor = e.currentTarget.dataset.bookauthor;
    var recommendImg = e.currentTarget.dataset.recommendimg;
    var recommendImgSource = e.currentTarget.dataset.recommendimgsource;
    var openid = e.currentTarget.dataset.openid;

      wx.request({
        url: that.data.selectRecommendURL,
        data: {
          bookName: bookName,
          bookAuthor: bookAuthor,
          openid: that.data.openId
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
           if(res.data.code==0){
             wx.showToast({
               title: "图书已推荐",
               image: '../../images/icon/fail.png',
               duration: 1500
             })
           }else{
             wx.navigateTo({
               url: "myRecommend/myRecommend?bookName=" + bookName + "&bookAuthor=" + bookAuthor + "&recommendToo=2" + "&recommendImg=" + recommendImgSource + "&nickName=" + nickName + "&avatarUrl=" + avatarUrl
             })
           }
        
        },
        fail: function (error) {
          // fail
          console.log(error)
        }
      })

    // if(that.data.openId==openid){
     
    // }else{
   
    // }
  
  },
  // in_feel: function (e) {
  //   var that = this;
  //   wx.chooseImage({
  //     count: 1,
  //     sizeType: ['original', 'compressed'],
  //     sourceType: ['album', 'camera'],
  //     success(photo) {
  //       wx.getImageInfo({
  //         src: photo.tempFiles[0].path,
  //         success: function (res) {
  //           var ctx = wx.createCanvasContext('photo_canvas');
  //           var ratio = 3;
  //           var canvasWidth = res.width
  //           var canvasHeight = res.height;
  //           that.setData({
  //             aaa: photo.tempFiles[0].path,
  //             canvasWidth2: res.width,
  //             canvasHeight2: res.height
  //           })
  //           // 保证宽高均在200以内
  //             while (canvasWidth > 200 || canvasHeight > 200) {
  //               //比例取整
  //               canvasWidth = Math.trunc(res.width / ratio)
  //               canvasHeight = Math.trunc(res.height / ratio)
  //               ratio++;
  //             }
  //           that.setData({
  //             canvasWidth: canvasWidth,
  //             canvasHeight: canvasHeight
  //           })//设置canvas尺寸
  //           ctx.drawImage(photo.tempFiles[0].path, 0, 0, canvasWidth, canvasHeight)  //将图片填充在canvas上
  //           ctx.draw()

  //           //下载canvas图片
  //           setTimeout(function () {
  //             wx.canvasToTempFilePath({
  //               canvasId: 'photo_canvas',
  //               quality:1,
  //               width:that.data.canvasWidth,
  //               height: that.data.canvasHeight,
  //               success: function (resn) {
  //                 console.log(resn.tempFilePath)
  //                 that.setData({
  //                   fillFilePaths: resn.tempFilePath
  //                 })
  //               },
  //               fail: function (error) {
  //                 console.log(error)
  //               }
  //             })
  //           }, 100)
  //         },
  //         fail: function (error) {
  //           console.log(error)
  //         }
  //       })

  //     },
  //     fail: function (error) {
  //       // fail
  //       wx.showToast({
  //         title: '未选取',
  //         duration: 1000,
  //         image: '../../images/icon/fail.png',
  //       })
  //     }
  //   })
  // },

//   //获取用户输入的用户名
//   bookName: function (e) {
//     this.setData({
//       bookName: e.detail.value
//     })
//   },
//   author: function (e) {
//     this.setData({
//       bookAuthor: e.detail.value
//     })
//   },
// //确定
//   new1: function (e) {
//     var that = this;
   
//     if (that.data.fillFilePaths != "" && that.data.bookName != "" && that.data.bookAuthor != "") {
//       wx.uploadFile({
//         url: that.data.recommendURL,// 仅为示例，非真实的接口地址
//         filePath: that.data.fillFilePaths,
//         name: 'file',
//         formData: {
//           bookName: that.data.bookName,
//           bookAuthor: that.data.bookAuthor,
//           openid: that.data.openId
//         },
//         success(res) {
//           var data = res.data;
//           const strToObj = JSON.parse(data);
//           var code = strToObj.code;
//           var msg = strToObj.msg;
//           if(code == 0){
//             wx.showToast({
//               title: msg,
//               duration: 1000,
//               icon: 'success'
//             })
//             that.onPullDownRefresh(1, "yes");
//           }else{
//             wx.showToast({
//               title: msg,
//               duration: 1750,
//               image: '../../images/icon/fail.png',
//             })
//           }
//           // do something
          
//         },
//         fail: function (error) {
//           // fail
//           wx.showToast({
//             title: '上传失败',
//             duration: 1000,
//             image: '../../images/icon/fail.png',
//           })
//         }
//       })
//       this.setData({
//         showModalStatus: false
//       })
//       this.setData({
//         bookName: "",
//         bookAuthor: "",
//         fillFilePaths: "",
//       })
//     }else{
//       wx.showToast({
//         title: '请填写完整',
//         duration: 1000,
//         image: '../../images/icon/fail.png',
//       })
//     }
//     // var currentStatu = e.currentTarget.dataset.statu;
//     // this.util(currentStatu);
 

   
//   },

  //我要推荐
  // powerDrawer: function (e) {
  //   // var currentStatu = e.currentTarget.dataset.statu;
  //   // this.util(currentStatu)
  //     this.setData({
  //       showModalStatus: true
  //     });
  // },

  // close: function(e) {
  //   this.setData({
  //     showModalStatus: false
  //   })

  // },


})







